var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;
var ManageCashStatus = 2;
var ManageCashId = 0;

var systemManageCashFuntion = function () {
    $.ajax({
        url:"usersWeb/findAllCardLogByUserIdPage.json",
        type:'POST',
        dataType:"json",
        data:{
            userid:0,page:page,pageNum:pageNum,status:'2'
        },
        success:function(result){
            var tem = "<tr>" +
                "<th>编号</th>" +
                "<th>用户</th>" +
                "<th>用户手机</th>" +
                "<th>提现金额</th>" +
                "<th>提现银行</th>" +
                "<th>提现账户</th>" +
                "<th>状态</th>" +
                "<th>描述</th>" +
                "<th>申请时间</th>" +
                "<th>操作</th>" +
                "</tr>";
            $.each( result.cardLogList, function(index, content){
                tem += "<tr>" +
                    "<td><p>"+content.id+"</p></td>" +
                    "<td><p>"+content.name+"</p></td>" +
                    "<td><p>"+content.creater+"</p></td>"+
                    "<td><p>"+content.money+"</p></td>"+
                    "<td><p>"+content.cardname+"</p></td>"+
                    "<td><p>"+content.cardnumber+"</p></td>";
                if(content.status == '1'){
                    tem +="<td><p>提现成功</p></td>";
                }else if(content.status == '2'){
                    tem +="<td><p>处理中</p></td>";
                }else if(content.status == '3'){
                    tem +="<td><p>不同意</p></td>";
                }else {
                    tem +="<td><p>未知状态</p></td>";
                }
                tem +="<td><p></p></td>" +
                    "<td><p>"+content.createtime+"</p></td>"+
                    "<td><h2><a class='ico deleteEm' onclick=\"manageCashOK("+content.id+");\">同意提现</a>" +
                    "<a class='ico deleteEm' onclick=\"manageCashNO("+content.id+");\">不同意</a></h2>" +
                    "</tr>";
            });
            $("#systemManageCashTable").empty();
            $("#systemManageCashTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageI = 1;
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemManageCashPage").empty();
            $("#systemManageCashPage").append(temPage);

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });

            //修改
            $(".deleteEm").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".roleDele").css({
                    top:($(window).height()-$(".roleDele").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

        }
    });
};
systemManageCashFuntion();

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemManageCashFuntion();
    pageOld = o;
};

//同意
var manageCashOK = function(id){
    ManageCashStatus = 1;
    ManageCashId = id;
};

//不同意
var manageCashNO = function(id){
    ManageCashStatus = 3;
    ManageCashId = id;
};

//修改提交
$("#systemManageCashSubmit").click(function(){
    var url = "appApiUser/updateCardLogById.json";
    $.ajax({
        url:url,
        type:'POST',
        dataType:"json",
        data:{
            id:ManageCashId,status:ManageCashStatus,updater:loginUserName
        },
        success:function(result){
            navPills('systemManageCashHtml');
            if(result.status == '200'){
                hintsPValueFucntion(result.m);
            }else {
                hintsPValueFucntion(result.msg);
            }
        },
        error:function(){
            hintsPValueFucntion("请求失败");
        }
    });
});

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
};

//关闭
$(".cancel,.confirm").click(function(){
    closeDivFunction();
});

var closeDivFunction = function (){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".authorityResc").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
};